package com.wps.netConnection
{
	import com.wps.data.NetData;
	import com.wps.netConnection.rpc.RemoteService;
	import com.wps.netConnection.rpc.RpcToken;
	import com.wps.netConnection.rpc.event.RpcEvent;
	
	import flash.net.sendToURL;
	
	import mx.messaging.ChannelSet;
	import mx.messaging.channels.AMFChannel;
	import mx.rpc.AbstractOperation;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;

	public class NetMessageSystem
	{
		private static const MethodLogin: String			= "Login";
		private static const MethodReLogin: String		= "ReLogin";
		private static const MethodLogout: String			= "Logout";
		private static const MethodInitData: String = "initData";
		
//		private static const serverAddr: String			= "http://113.74.35.2:4955/burndown/";
		private static const serverAddr: String			= "http://113.df";
		private static const Destination: String			= "";
		
		//--------------------------------------------------------------------------
		//
		//  变量
		//
		//--------------------------------------------------------------------------
		
		private var _service: RemoteService = null;
		private var _userName: String = "";
		private var _sessionID: String = "";
		
		private var _tokens: Vector.<RpcToken> = new Vector.<RpcToken>();
		private var _callbacks: Vector.<Function> = new Vector.<Function>();
		private var _handlers: Object = {};
		
		public function NetMessageSystem()
		{
			_service = new RemoteService(serverAddr, Destination);
			_service.addEventListener(RpcEvent.WOW_RPC_EVENT, _rpcEventHandler);	
			
			_handlers[MethodLogin] = _loginResultHandler;
			_handlers[MethodInitData] = _initDataHandler;
		}
		
		
		/**
		 * 		登陆
		 * 		callback : function loginHandler( code: int)
		 */		
		public function login(callback: Function, userName: String, password: String, bSaveCookie: Boolean): int
		{
			_sessionID = "";
			var token: RpcToken = _callMethod(MethodLogin, 
				{"UserName": userName, "Password": password}, callback);
			token.userData = bSaveCookie;
			return token.tokenIndex;
			
		}
		
		/**
		 * @private
		 */
		private function _loginResultHandler(tokenIndex: int, info: RpcCallBackInfo, callback: Function): void
		{
				trace(info.code);
			trace("login login login");
			callback(info.code);	
		}
		
		
		public function initData(callback: Function): int
		{
//			var channel:AMFChannel = new AMFChannel("pyamf", "  http://113.74.35.2:4955/burndown/");
//			var channels:ChannelSet = new ChannelSet();
//			channels.addChannel(channel);
//			
//			var remoteObj: RemoteObject = new RemoteObject("BurndownService");
//			remoteObj.channelSet = channels;
////			remoteObj.addEventListener(FaultEvent.FAULT, onFault);s
//
//			var op:AbstractOperation = remoteObj.getOperation("getGraphDataAll");
//			op.addEventListener(ResultEvent.RESULT, onInitData);
////			op.send("azhe","AAAAA");
//			op.send();
//			
//			function onInitData(event: ResultEvent): void
//			{
//				trace(event.target);
//			}
			
			return _callMethod(MethodInitData, null, callback).tokenIndex;
		}
		
		private function _initDataHandler(tokenIndex: int, info: RpcCallBackInfo, callback: Function): void
		{
			NetData.instance.importFormXml(null);
			callback(info.code);
		}
		
		//--------------------------------------------------------------------------
		//
		//  文档库基础函数
		//
		//--------------------------------------------------------------------------
		
		/**
		 * 调用文档库方法的辅助方法。
		 * @param method 方法名。
		 * @param params 参数。
		 * @param callback 方法调用完成的回调函数。
		 */
		private function _callMethod(method: String, params: Object, callback: Function): RpcToken
		{
			//			var token: RpcToken = _service.call(method, {"Header": array1, "Postdata": array3});
			var token: RpcToken = _service.call(method, {"v": 1.0, "sid": _sessionID, "param": params});
			_tokens.push(token);
			_callbacks.push(callback);
			return token;
		}
		
		/**
		 * 文档库方法回调函数总入口。
		 * @param event rpc事件，包含方法调用参数和结果、状态等信息。
		 */
		private function _rpcEventHandler(event: RpcEvent): void
		{
			var token: RpcToken = RpcToken(event.token);
			var index: int = _tokens.indexOf(token);
			if (index != -1)
			{
				var code: uint = event.result;
				var result: Object = null;
				if(event.result == RpcEvent.StatusCodeSucceed)
				{
					result = event.data["result"];
				}
				
				var info: RpcCallBackInfo = new RpcCallBackInfo(code, result, token.args["param"], token.userData);
				_handlers[token.method](token.tokenIndex, info, _callbacks[index]);
				
				_tokens.splice(index, 1);
				_callbacks.splice(index, 1);
			}
		}
	}
}





class RpcCallBackInfo
{
	public var code: uint;
	public var result: Object;
	public var param: Object;
	public var userData: Object;
	
	public function RpcCallBackInfo(code: uint, result: Object, param: Object, userData: Object)
	{
		this.code = code;
		this.result = result;
		this.param = param;
		this.userData = userData;
	}
}
